package weaver.interfaces.dsjj.duss.mailCronJob;

import weaver.conn.BatchRecordSet;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.general.Util;
import weaver.interfaces.datasource.DataSource;
import weaver.interfaces.dsjj.duss.TransMethod;
import weaver.interfaces.schedule.BaseCronJob;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-06-05
 * @Description: ${description}
 * @ModifyLog:
 **/

public class MailInfoSynMid extends BaseCronJob {
    @Override
    public void execute() {
                RecordSet rs = new RecordSet();
                RecordSet rs1 = new RecordSet();
                RecordSet rs2 = new RecordSet();
                BaseBean baseBean = new BaseBean();
                baseBean.writeLog("in----MailInfoSynMid");
                BatchRecordSet batchRecordSet = new BatchRecordSet();
                String time = TransMethod.getCurTime();
                String timebefore = TransMethod.getBeforeTime();
                baseBean.writeLog("timebefore="+timebefore+",curtime="+time);
                List<List<Object>> resultList = new ArrayList<List<Object>>();
                try {
                    String SELECT_SQL = "select r.id,r.mit_uuid,r.sendfrom,r.senddate,r.size_n,r.resourceid," +
                            "r.subject,r.content,r.mailtype,r.needreceipt,r.status," +
                            "f.filename,substring_index(filename,'.',-1) as filetype,f.iszip,f.isencrypt,f.filesize,f.filerealpath, " +
                            "t.MAILID,t.mailcontent from mailresource r " +
                            "left join mailresourcefile f on r.ID=f.MAILID " +
                            "left join mailcontent t on r.id=t.MAILID  where r.senddate between '" + timebefore + "' and '" +time+ "' and ((sendcc_bak is null) or (sendcc_bak =''))";
                    rs.executeQuery(SELECT_SQL);
                    while (rs.next()) {
                        List<Object> list = new ArrayList<>();
                        String mr_uuid = Util.null2String(rs.getString("mit_uuid"));//uuid
                        String sendfromid = Util.null2String(rs.getString("sendfrom"));//发送人
                        String senddate = Util.null2String(rs.getString("senddate"));//发送日期
                        String size_n = Util.null2String(rs.getString("size_n"));//邮件大小
                        String resourceid = Util.null2String(rs.getString("resourceid"));//接收人
                        String subject = Util.null2String(rs.getString("subject"));//主题
                        String content = Util.null2String(rs.getString("content"));//内容
                        String mailtype = Util.null2String(rs.getString("mailtype"));//邮件类型
                        String needreceipt = Util.null2String(rs.getString("needreceipt"));//是否回执
                        String status = Util.null2String(rs.getString("status"));//状态
                        String filename = Util.null2String(rs.getString("filename"));//文件名
                        String filetype = Util.null2String(rs.getString("filetype"));//文件类型
                        String iszip = Util.null2String(rs.getString("iszip"));//附件是否压缩
                        String isencrypt = Util.null2String(rs.getString("isencrypt"));//附件是否加密
                        String filesize = Util.null2String(rs.getString("filesize"));//附件大小
                        String filepath = Util.null2String(rs.getString("filerealpath"));//附件路径
                        String mailid = Util.null2String(rs.getString("mailid"));//附件路径
                        String mailcontent = Util.null2String(rs.getString("mailcontent"));//文件内容
                        String sendPersonGuid = TransMethod.transIDtoGUID(sendfromid);//发件人GUID
                        String sendfrom = TransMethod.transIDtoLastname(sendfromid);//发件人名称
                        String receivePersonGuid = TransMethod.transIDtoGUID(resourceid);//收件人GUID
                        String resource1 = TransMethod.transIDtoLastname(resourceid);//收件人名称
                        rs2.executeQuery("select * from yw_notepaper where mit_uuid='" + mr_uuid + "'");
                        baseBean.writeLog("select * from yw_notepaper where mit_uuid='" + mr_uuid + "'");
                        if (!rs2.next()) {
                        list.add(mr_uuid);
                        list.add(sendfromid);
                        list.add(sendPersonGuid);
                        list.add(sendfrom);
                        list.add(senddate);
                        list.add(size_n);
                        list.add(resourceid);
                        list.add(receivePersonGuid);
                        list.add(resource1);
                        list.add(subject);
                        list.add(content);
                        list.add(mailtype);
                        list.add(needreceipt);
                        list.add(status);
                        list.add(mailid);
                        list.add(mailcontent);
                        list.add(filename);
                        list.add(filetype);
                        list.add(iszip);
                        list.add(isencrypt);
                        list.add(filesize);
                        list.add(filepath);
                        list.add("EMPTY_BLOB()");
                        resultList.add(list);

                        }

                    }
                    batchRecordSet.executeBatchSql("insert into mail_middleinfo(mr_uuid,sendfromid,sendPersonGuid,sendfrom,senddate,size_n,resourceid,receivePersonGuid,resource,subject,content,\n" +
                            "mailtype,needreceipt,status,mailid,mailcontent,filename,filetype,iszip,isencrypt,filesize,filepath,filecontent) " +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", resultList);

                    rs1.executeQuery("select id,mailid,filepath,iszip from mail_middleinfo where senddate between '" + timebefore + "' and '" + time + "'");
                    while (rs1.next()) {
                        String id = Util.null2String(rs1.getString("id"));
                        String mailid = Util.null2String(rs1.getString("mailid"));
                        String filepath = Util.null2String(rs1.getString("filepath"));
                        String iszip = Util.null2String(rs1.getString("iszip"));
                        if (!"".equals(filepath) && !"".equals(mailid)) {
                            queryMailFile(filepath, iszip, id);
                        }
                    }
                    rs.executeUpdate("update syntimedocinfo set beforetime = '"+timebefore+"',curtime='"+time+"' where id ='1'");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    public static void queryMailFile(String filepath, String iszip, String id) throws Exception {
        DataSource ds = (DataSource) StaticObj.getServiceByFullname(("datasource.local"), DataSource.class);
        BaseBean bb = new BaseBean();
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = " update mail_middleinfo set filecontent=? where id=?";
            PreparedStatement pstm = conn.prepareStatement(sql);
            try {
                bb.writeLog("id:" + id);
                bb.writeLog("filepath:" + filepath);
                bb.writeLog("iszip:" + iszip);
                InputStream inputstream = null;
                ZipInputStream zin = null;
                try {
                    File docfile = new File(filepath);
                    if ("0".equals(iszip)) {
                        inputstream = new BufferedInputStream(new FileInputStream(docfile));
                    } else {
                        zin = new ZipInputStream(new FileInputStream(docfile));
                        if (zin.getNextEntry() != null) {
                            inputstream = new BufferedInputStream(zin);
                        }
                    }
                    pstm.clearParameters();
                    pstm.setBinaryStream(1, inputstream);
                    pstm.setString(2, id);
                    pstm.addBatch();
                    pstm.executeBatch();
                    if (zin != null) {
                        zin.close();
                    }

                    if (inputstream != null) {
                        inputstream.close();
                    }
                } catch (Exception e) {
                    bb.writeLog(e.getMessage());
                } finally {
                    if (inputstream != null) {
                        inputstream.close();
                    }

                    if (zin != null) {
                        zin.close();
                    }
                }
                conn.close();
            } catch (Exception e) {
                bb.writeLog(e.getMessage());
            } finally {
                try {
                    conn.close();
                } catch (Exception e) {

                }
            }
        } catch (Exception e) {

        }
    }
}

